package com.example.administrator.nurse.widget;

import android.content.Context;
import android.media.Image;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.nurse.MainActivity;
import com.example.administrator.nurse.R;
import com.example.administrator.nurse.util.ViewFactory;

import java.util.ArrayList;
import java.util.InputMismatchException;

/**
 * Created by Administrator on 2016/4/3.
 */
public class CycleViewPager extends ViewPager{

    ArrayList<ImageView> imgList;
    ArrayList<ImageView> indicatorList;
    int indicatorSize;
    BannerViewPagerAdapter mAdapter;
    PagerChangeListener mListener;
    Context context;
    LinearLayout indicator;
    int nowPosition=0;
    int movePosition=0;
    long timeout=0;
    int move=0x111;
    public Thread moveThread=new Thread(){
        @Override
        public void run() {
            while (true){
                try {
                    sleep(timeout);
                    Message message=handler.obtainMessage();
                    message.arg1=movePosition;
                    message.what=move;
                    handler.sendMessage(message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==move){
                int position=msg.arg1;
                Log.d("nnnnn","position="+position+"  size="+imgList.size());
                int max=imgList.size()+1;
                int post=(position+1)%imgList.size();
                CycleViewPager.this.setCurrentItem(post,true);
                if (post==max){
                        CycleViewPager.this.setCurrentItem(1,false);
                    }

                    Log.d("nnnnn", "position1=" + position);

            }
        }
    };

    public CycleViewPager(Context context) {
        super(context);
        this.context=context;
        addOnPageChangeListener(null);
    }

    public CycleViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        addOnPageChangeListener(null);
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        mAdapter=new BannerViewPagerAdapter();
        super.setAdapter(mAdapter);
        setCurrentItem(1);
    }

//    @Override
//    public void setCurrentItem(int item) {
//        super.setCurrentItem(item);
////        ImageView imageView= (ImageView) indicator.getChildAt(item);
////        imageView.setImageResource(R.drawable.indicaterect);
//    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    @Override
    public void addOnPageChangeListener(OnPageChangeListener listener) {
        mListener=new PagerChangeListener();
        super.addOnPageChangeListener(mListener);
    }

    public void setImgList(ArrayList<ImageView> imgList) {
        this.imgList = new ArrayList<ImageView>();
        this.imgList.add(imgList.get(imgList.size() - 1));
//        indicator= (LinearLayout) ((MainActivity)context).findViewById(R.id.indicator);
        for (ImageView view:imgList){
            this.imgList.add(view);
//            ImageView imageView=new ImageView(context);
//            imageView.setImageResource(R.drawable.indicatering);
//            indicator.addView(imageView);
        }
        this.imgList.add(imgList.get(0));
    }
    public void setImgList(Context context,ArrayList<Integer> imgidList){
        this.imgList = new ArrayList<ImageView>();
        this.imgList.add(ViewFactory.getImageView(context,imgidList.get(imgidList.size()-1)));
        indicator= (LinearLayout) ((MainActivity)context).findViewById(R.id.indicator);
        indicator.setOrientation(LinearLayout.HORIZONTAL);
        this.indicatorList=new ArrayList<ImageView>();
        LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(10,0,10,0);
        indicator.removeAllViews();
        for (Integer id:imgidList){
            this.imgList.add(ViewFactory.getImageView(context, id));
//            CircleIndicator point=new CircleIndicator(context);
            ImageView point=new ImageView(context);
            point.setLayoutParams(lp);
            indicatorList.add(point);
            indicator.addView(point);
        }
        indicatorSize=indicatorList.size();
//        setIndicator(1);
        this.imgList.add(ViewFactory.getImageView(context,imgidList.get(0)));
    }
    private class PagerChangeListener implements OnPageChangeListener{
        int mPosition;
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.d("nnnnnn","-----onPageSelected------+position="+position);
            mPosition=position;
            movePosition=position;
            int size=indicatorList.size();
//            if (position==0){
//                setIndicator(size-1);
//            }else if(position<=size){
//                setIndicator(position-1);
//            }else if (position==imgList.size()-1){
//                setIndicator(0);
//            }else {
//
//            }
            setIndicator(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state==ViewPager.SCROLL_STATE_IDLE) {
                Log.d("nnnnnn", "-----onPageScrollStateChanged------");
                if (mPosition==mAdapter.getCount()-1){
                    setCurrentItem(1,false);
                }else  if (mPosition==0){
                    setCurrentItem(mAdapter.getCount()-2,false);
                }
            }
        }
    }

//    private void setIndicator(int selectePosition){
//        if(selectePosition==0){
//            indicatorList.get(selectePosition).setToState(CircleIndicator.TORECT);
//            indicatorList.get(indicatorSize-1).setToState(CircleIndicator.TOPOINT);
//        }else if (selectePosition==(indicatorSize-1)){
//            indicatorList.get(selectePosition).setToState(CircleIndicator.TORECT);
//            indicatorList.get(0).setToState(CircleIndicator.TOPOINT);
//        }else{
//            indicatorList.get(selectePosition).setToState(CircleIndicator.TORECT);
//            indicatorList.get(selectePosition-1).setToState(CircleIndicator.TOPOINT);
//        }
//    }
private void setIndicator(int selectePosition){
    for (int i=0;i<indicatorList.size();i++){
        indicatorList.get(i).setImageResource(R.drawable.indicatering);
    }
    if (selectePosition==0){
        indicatorList.get(indicatorList.size()-1).setImageResource(R.drawable.indicaterect);
    }else if (indicatorList.size()>=selectePosition){
        Log.d("nnnnnn", "-----setIndicator1-----");
        indicatorList.get(selectePosition-1).setImageResource(R.drawable.indicaterect);
    }else if (selectePosition==imgList.size()-1){
        Log.d("nnnnnn","-----setIndicator2-----");
        indicatorList.get(0).setImageResource(R.drawable.indicaterect);
    }
}
//    private void setIndicator(int selectePosition){
//        for (int i=0;i<indicatorList.size();i++){
//            indicatorList.get(i).setToState(CircleIndicator.TOPOINT);
//        }
//        if (selectePosition==0){
//            indicatorList.get(indicatorList.size()-1).setToState(CircleIndicator.TORECT);
//        }else if (indicatorList.size()>=selectePosition){
//            Log.d("nnnnnn", "-----setIndicator1-----");
//            indicatorList.get(selectePosition-1).setToState(CircleIndicator.TORECT);
//        }else if (selectePosition==imgList.size()-1){
//            Log.d("nnnnnn","-----setIndicator2-----");
//            indicatorList.get(0).setToState(CircleIndicator.TORECT);
//        }
//    }
    private class BannerViewPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imgList.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
//            if (position==1){
//                position=this.getCount()-1;
//                container.addView(imgList.get(position));
//            }else if(position==this.getCount()+1){
//                position=0;
//                container.addView(imgList.get(position));
//            }
//            else{
//                position=-1;
//            }
            ((ViewPager)container).addView(imgList.get(position));
//            return super.instantiateItem(container,position);
            return imgList.get(position);
        }



        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager)container).removeView(imgList.get(position));
//            super.destroyItem(container, position, object);
        }
    }
}
